package leetcode.array;

import java.util.HashMap;

/**
 * @author Cheng Jun
 * Description: https://leetcode.cn/problems/integer-to-roman/
 * @version 1.0
 * @date 2022/7/18 19:28
 * @see romanToInt
 */
public class intToRoman {
    private static HashMap<Integer, String> romanNumMap = new HashMap<>(16);

    public static void main(String[] args) {
        // 创建一份字典，字符和数值的对应关系
        romanNumMap.put(1, "I");
        romanNumMap.put(4, "IV");
        romanNumMap.put(5, "V");
        romanNumMap.put(9, "IX");
        romanNumMap.put(10, "X");
        romanNumMap.put(40, "XL");
        romanNumMap.put(50, "L");
        romanNumMap.put(90, "XC");
        romanNumMap.put(100, "C");
        romanNumMap.put(400, "CD");
        romanNumMap.put(500, "D");
        romanNumMap.put(900, "CM");
        romanNumMap.put(1000, "M");
        System.out.println(intToRoman(1994));
    }

    static String intToRoman(int num) {
        StringBuilder res = new StringBuilder("");
        // num = 1994 "MCMXCIV"
        int[] dicArr = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
        for (int i = 0; num > 0;){
            if (num >= dicArr[i]){
                num = num - dicArr[i];
                res.append(romanNumMap.get(dicArr[i]));
            }else {
                i++;
            }
        }
        return res.toString();
    }
}
